;
(function () {
  new Vue({
    el: '#dd_unit_rent_details_report',
    data: {
      quanxian: JSON.parse(localStorage.getItem('permissions')),
      pageCounts: [{
          'name': '10',
          'code': 10
        },
        {
          'name': '15',
          'code': 15
        },
        {
          'name': '20',
          'code': 20
        },
      ],
      currentPage: 1,
      totalPage: 0,
      totalCount: 0,
      limit: ['10'],

      rentList:[{'name':'全部','code':'#'},{'name':'已租','code':'Y'},{'name':'未租','code':'N'}],
      selectedRentStatus: ['#'],
      selectedRentStatusName: '',
      projectList: [],
      selectedProId: '',
      selectedProName: '',
      projectIdSelect: ['#'],
			projectIds: [],

      keyword: '',
      billDate: '',
      reportDate: '',
      contractYearsStart: '',
      contractYearsEnd: '',
      selectedRent: [],
      dataList: [],
			loadingFlag: true
    },
    filters: {
      formatyyyyMMdd: function (time) {
				if (!time) return '';
        return hlDateUtil.formatUTCDate(time, 'yyyy-MM-dd')
      },
      formatNum: function (num) {
        if (!num) return '0.00'
        return hlNumberUtil.formatNum(num)
      },
    },
    computed: {

    },
    methods: {
      // 返回当前前n天的日期
      lastDayDate(count) {
        var dd = new Date();
        dd.setDate(dd.getDate() + count); //获取count天后的日期
        var y = dd.getFullYear();
        var m = dd.getMonth() + 1;//获取当前月份的日期
        var d = dd.getDate();

        function tzero(n) {
          return n < 10 ? '0' + n : '' + n
        }

        return y + "-" + tzero(m) + "-" + tzero(d);
      },
      // 获取列表数据
      getData() {
        let data = {
          project_id: this.projectIdSelect[0] || '#',
          report_date: this.reportDate || '#',
          rent_status: this.selectedRentStatus.length ? this.selectedRentStatus[0] : '#',
          contract_years_start: this.contractYearsStart || '#',
          contract_years_end: this.contractYearsEnd || '#',
          page: this.currentPage,
          limit: this.limit + '',
        }

        hlhttp.get({
          url: 'report/unit/unit_rent_detail_daily_map',
          data: data,
          success: res => {
            this.totalPage = res.totalPage
            this.totalCount = res.totalCount

            res.datas.forEach((item, i) => {
              item['rentResourceList'] = item['rentSource'] ? item['rentSource'].split(',') : []
            });

            this.dataList = res.datas
						this.loadingFlag = false
          }
        })

      },
      // 获取所有项目
      getProject() {
        hlhttp.get({
          url: 'user/projects',
          success: (res) => {
            res = hlArrayUtil.removeRepeat(res, 'projectId')
						res.map(item => {
							this.projectIds.push(item.projectId)
						})
            res.unshift({
              projectName: '所有项目',
              projectId: '#'
            })

            this.projectList = hlArrayUtil.replaceData(res, [{ key: 'projectName' , rKey: 'name'}, { key: 'projectId' , rKey: 'code'}])

            console.log(this.projectList);

						this.getData()
          }
        })

      },
      // 选择某个项目
      getProjectItem(arr) {
        this.selectedProId = arr[0].code
        this.selectedProName = arr[0].name
      },
      getRentItem(arr) {
        this.selectedRentStatus = [arr[0].code]
        this.selectedRentStatusName = arr[0].name
      },
      // 查询
      search() {

				this.currentPage = 1
				this.loadingFlag = true
				this.getData()
      },
      // 重置
      reset() {
				this.loadingFlag = true
        this.projectIdSelect = ['#']
        this.selectedProName = ''
        this.reportDate = this.lastDayDate(-1)
        this.contractYearsStart = ''
        this.contractYearsEnd = ''
        this.selectedRentStatus = ['#']
        this.selectedRentStatusName = ''
        this.currentPage = 1
        this.getData()
      },
      // 导出
      excl() {
				hlhttp.get({
				  baseUrl: exclUrl,
				  url: 'report/unit/unit_rent_detail_daily_map/list_export',
				  data: {
            project_id: this.projectIdSelect[0] || '#',
            report_date: this.reportDate || '#',
            rent_status: this.selectedRentStatus.length ? this.selectedRentStatus[0] : '#',
            contract_years_start: this.contractYearsStart || '#',
            contract_years_end: this.contractYearsEnd || '#',
				  },
				  success: (data) => {
				    var a = document.createElement('a');
				    a.href = data.excel_url;
				    $('body').append($(a))
				    a.click();
				    setTimeout(function() {
				      $(a).remove()
				    }, 100);

				  }
				})
      },
    },
    created() {
      this.reportDate = this.lastDayDate(-1)
    },
    mounted() {
      this.getProject()
    }
  })

}())
